<? 
	$title="Edit User"; 
	$mid="4";
	
	require("required/dbconnect.php"); 
	require_once("required/function.php");
	
	checkAccess("allow_user_management",1);
	
	if(isset($_POST['btnsubmit']))
	{
		$id=$_POST['id'];
		
		$user_password=mysql_escape_string($_POST['user_password']);
		$user_role=$_POST['user_role'];
		$branch_id=$_POST['branch_id'];
		$status=$_POST['status'];
		
		if($user_password!="")
		{
			$password_q=",user_password='$user_password'";
		}
		else
		{
			$password_q="";
		}
		
		$query_reset=mysql_query("update os_user set user_password='$user_password',role_id='$user_role',branch_id='$branch_id',status='$status' $password_q where id=$id");
		
		header("Location:user_view_all.php?action=3");
		
	}
	else
	{
	
		require("required/header.php");	
		$id=$_GET['id'];		
		
		$get_data=mysql_query("select * from os_user where id=$id");
		$a=mysql_fetch_array($get_data,MYSQL_ASSOC);
		
		
		
?>
<script>
	function frmvalidate(editfrm)
	{
		if(document.editfrm.user_name.value=="")
		{
			alert("Please insert user name");
			document.editfrm.user_name.focus();
			return false;
		}
		
		if(document.editfrm.user_password.value!="")
		{
			
			if(document.editfrm.user_password.value.length<6)
			{
				alert("Password length must be more than 6 characters");
				document.editfrm.user_password.focus();
				return false;
			}
			if(document.editfrm.user_password.value.indexOf(" ")>-1)
			{
				alert("No spacing allowed!");
				document.editfrm.user_password.focus();
				return false;
			}
			if(document.editfrm.confirm_password.value=="")
			{
				alert("Please re-enter your password");
				document.editfrm.confirm_password.focus();
				return false;
			}
			if(document.editfrm.user_password.value!=document.editfrm.confirm_password.value)
			{
				alert("Password doesn't match, please check");
				document.editfrm.confirm_password.focus();
				return false;
			}
		}
		
		if(document.editfrm.user_role.value=="")
		{
			alert("Please select an user role");
			document.editfrm.user_role.focus();
			return false;
		}
		return true;
	}
	
	
	
</script>
<table width="100%">

    <td valign="top" align="left">
       <table width="95%" align="center">
	
	<tr>
		<td class="title"><?= $title ?></td>
	</tr>
	<tr>
		<td colspan="3" align="left">
		<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="editfrm" onSubmit="return frmvalidate(this)">
        <input type="hidden" value="<?= $id ?>" name="id" />
        <table cellpadding="5">
            <tr>
                <td>User Name</td><td> : <input type="text" name="user_name" size="30" maxlength="100" value="<?= $a['user_id'] ?>" readonly="readonly" disabled="disabled"></td>
            </tr>
            <tr>
                <td>Password </td><td>  : <input type="password" name="user_password" size="30" maxlength="100" value=""/></td>
            </tr>
            <tr>
                <td>Confirm Password </td><td>  : <input type="password" name="confirm_password" size="30" maxlength="100" value="" /></td>
            </tr>
              <tr>
            	<td colspan="2">*leave the password field blank if no changes are required</td>
            </tr>
            
            <tr>
			
            <tr>
                <td>Role</td><td>  : 
                <select name="user_role">
                    <option value="">Please Select</option>
                    <?
                        $getRole=mysql_query("select * from os_user_role order by role_name asc");
                        
                        if(mysql_num_rows($getRole)>0)
                        {
                            while($r=mysql_fetch_array($getRole,MYSQL_ASSOC))
                            {
								if($r['id']==$a['role_id'])
								{
									$roleslt="selected";
								}
								else
								{
									$roleslt="";
								}
                    ?>
                    <option value="<?= $r['id'] ?>" <?= $roleslt ?>><?= $r['role_name'] ?></option>
                    <? 
                            }
                        } 
                    ?>
                </select>
                </td>
            </tr>
            
             <tr>
                <td>Status</td><td>  : 
                <select name="status">
                	<?
						if($a['status']==1)
						{
							$status1="selected";
							$status2="";
						}else{
							$status1="";
							$status2="selected";
						}
					?>
                    <option value="0" <?= $status2 ?>>Inactive</option>
                    <option value="1" <?= $status1 ?>>Active</option>
                </select>
                </td>
            </tr>

          
        </table>
         <input type="hidden" name="branch_id" value="0">
        <input type="submit" value="Update" name="btnsubmit">
        <input type="button" value="Cancel" onclick="history.go(-1)">
  </form>
		</td>
	</tr>
	</table>
	</td>
 
</table>
<? } require("required/footer.php"); ?>
